package com.lj.demo.springbootshirojwt.entity;

import com.google.common.base.Objects;
import com.lj.demo.springbootshirojwt.util.Constants;

import java.io.Serializable;

/**
 * Title: 返回结果类
 * Description:
 * Date: 2019年06月26日 10:22
 *
 * @author lvjie@hiynn.com
 * @version 1.0
 * Significant Modify：
 * Date                  Author                 Content
 * =================================================================
 * 2019年06月26日         lvjie@hiynn.com         创建文件,实现基本功能
 * =================================================================
 */
public class ResponseResult<T> implements Serializable {

    private int code;

    private String message;

    private T result;

    private ResponseResult(){

    }

    public ResponseResult(int code,String message, Object result){
        this.code = code;
        this.message = message;
        this.result = (T) result;
    }

    public static ResponseResult success(Object result){
        return new ResponseResult(Constants.RESPONSE_SUCCESS_CODE,Constants.RESPONSE_SUCCESS_MESSAGE,result);
    }

    public static ResponseResult success(){
        return new ResponseResult(Constants.RESPONSE_SUCCESS_CODE,Constants.RESPONSE_SUCCESS_MESSAGE,null);
    }

    public static ResponseResult fail(Object result){
        return new ResponseResult(Constants.RESPONSE_FAIL_CODE,Constants.RESPONSE_FAIL_MESSAGE,result);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getResult() {
        return result;
    }

    public void setResult(T result) {
        this.result = result;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ResponseResult<?> that = (ResponseResult<?>) o;
        return code == that.code &&
                Objects.equal(message, that.message) &&
                Objects.equal(result, that.result);
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(code, message, result);
    }

    @Override
    public String toString() {
        return "ResponseResult{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", result=" + result +
                '}';
    }
}
